"""
评论模型
映射数据库comments表
"""
from sqlalchemy import Column, Integer, Text, DateTime, ForeignKey, Index
from sqlalchemy.dialects.mysql import TINYINT
from sqlalchemy.orm import relationship

from app.models.base import BaseModel


class Comment(BaseModel):
    """
    评论模型类
    对应数据库中的comments表
    """
    
    __tablename__ = "comments"
    
    id = Column(
        Integer, 
        primary_key=True, 
        autoincrement=True,
        comment='评论ID'
    )
    
    content = Column(
        Text, 
        nullable=False,
        comment='评论内容'
    )
    
    like_count = Column(
        Integer, 
        nullable=False, 
        default=0,
        comment='点赞数'
    )
    
    status = Column(
        TINYINT, 
        nullable=False, 
        default=1,
        comment='状态：1-正常，0-禁用'
    )
    
    user_id = Column(
        Integer, 
        ForeignKey('users.id'), 
        nullable=False,
        index=True,
        comment='评论用户ID'
    )
    
    video_id = Column(
        Integer, 
        ForeignKey('videos_detail.id'), 
        nullable=False,
        index=True,
        comment='视频ID'
    )
    
    parent_id = Column(
        Integer, 
        ForeignKey('comments.id'), 
        nullable=True,
        index=True,
        comment='父评论ID，为空表示顶级评论'
    )
    
    deleted_at = Column(
        DateTime, 
        nullable=True,
        index=True,
        comment='删除时间（软删除）'
    )
    
    # 关系
    user = relationship('User')
    video = relationship('Video', primaryjoin="Comment.video_id==Video.id")
    
    # 自关联关系（回复）
    parent = relationship('Comment', remote_side=[id], backref='replies')
    
    # 索引
    __table_args__ = (
        Index('idx_like_count', 'like_count'),
        Index('idx_status', 'status'),
    )